﻿using Architecture.Domain.Service;
using Architeture.Domain;
using Architeture.Domain.DTO;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.ZhuBoLin.Application.Command.RoleCommand;
using RBAC.ZhuBoLin.Application.Command.RoleCommand.Menu;
using RBAC.ZhuBoLin.Application.CommandHandlers.RoleHandler.Menu;
using RBAC.ZhuBoLin.Dtos;
using System.Security.Cryptography;

namespace RBAC.ZhuBoLin.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class MenuController : ControllerBase
    {

        protected readonly IMenuSerivice menuSerivice;
        protected readonly IMediator mediator;

        public MenuController(IMenuSerivice menuSerivice, IMediator mediator)
        {
            this.menuSerivice = menuSerivice;
            this.mediator = mediator;
        }


        /// <summary>
        /// 菜单显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<MenuPage>> GetMenuAll([FromQuery] MenuSelectCommand sd)
        {
            ApiResult<MenuPage> result = new ApiResult<MenuPage>();
            try
            {
                result.Data = await mediator.Send(sd);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 菜单添加
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> MennuAdd(MenuCreateCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(cd);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="sd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<MenuInfo>> GetMenuByid([FromQuery] MenuSetCommand sd)
        {
            ApiResult<MenuInfo> result = new ApiResult<MenuInfo>();
            try
            {
                result.Data = await mediator.Send(sd);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 菜单修改
        /// </summary>
        /// <param name="ud"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> MenuDelete(MenuUpdateCommand ud)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(ud);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteMenu(MenuDeleteCommand dd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(dd);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除判断
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<int>> MenuInfoDelis(int id)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await menuSerivice.MenuInfoDelis(id);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 返回菜单树形
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<ApiResult<List<mentumns>>> GetMenuTree()
        {
            ApiResult<List<mentumns>> result = new ApiResult<List<mentumns>>();
            try
            {
                result.Data = await mediator.Send(new GetMenuTreeCommand());
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 返回主页面菜单
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<ApiResult<List<MenuRoleTreeDto>>> GetRoleMenuTree(int mid = 0)
        {
            ApiResult<List<MenuRoleTreeDto>> result = new ApiResult<List<MenuRoleTreeDto>>();
            try
            {
                result.Data = await menuSerivice.GetRoleMenuTree(mid);
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 菜单下拉
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<MenuInfo>>> GetSelectMenu()
        {
            ApiResult<List<MenuInfo>> result = new ApiResult<List<MenuInfo>>();
            try
            {
                result.Data = await mediator.Send(new MenuSelectInfoCommand());
            }
            catch (Exception ex)
            {
                result.Code = 0;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}



